﻿using System;
using System.Runtime.InteropServices;
using TTEngine;

namespace dotnet.loader
{
    [StructLayoutAttribute(LayoutKind.Sequential, Pack = 1)]
    public struct myvb
    {
        //pos
        public float x;
        public float y;
        public float z;
        //uv
        public float u;
        public float v;
        //color
        public byte r;
        public byte g;
        public byte b;
        public byte a;
    }
    internal class MeshTool
    {
        public static Framework.handle_Mesh CreateRect()
        {
            unsafe
            {
                myvb[] vbdata = new myvb[12];
                vbdata[0] = new myvb { x = 0, y = 0, z = 1, u = 0, v = 0, r = 255, g = 255, b = 255, a = 255 };
                vbdata[1] = new myvb { x = 0.5f, y = 0, z = 1, u = 1, v = 0, r = 255, g = 255, b = 255, a = 255 };
                vbdata[2] = new myvb { x = 0, y = 0.5f, z = 1, u = 0, v = 1, r = 255, g = 255, b = 255, a = 255 };
                vbdata[3] = new myvb { x = 0, y = 0.5f, z = 1, u = 0, v = 1, r = 255, g = 255, b = 255, a = 255 };
                vbdata[4] = new myvb { x = 0.5f, y = 0, z = 1, u = 1, v = 0, r = 255, g = 255, b = 255, a = 255 };
                vbdata[5] = new myvb { x = 0.5f, y = 0.5f, z = 1, u = 1, v = 1, r = 255, g = 255, b = 255, a = 255 };
                vbdata[6] = new myvb { x = -1, y = -1, z = -1, u = 0, v = 0, r = 255, g = 255, b = 255, a = 255 };
                vbdata[7] = new myvb { x = -1, y = 1, z = -1, u = 1, v = 0, r = 255, g = 255, b = 255, a = 255 };
                vbdata[8] = new myvb { x = -1, y = -1, z = 1, u = 0, v = 1, r = 255, g = 255, b = 255, a = 255 };
                vbdata[9] = new myvb { x = -1, y = -1, z = 1, u = 0, v = 1, r = 255, g = 255, b = 255, a = 255 };
                vbdata[10] = new myvb { x = -1, y = 1, z = -1, u = 1, v = 0, r = 255, g = 255, b = 255, a = 255 };
                vbdata[11] = new myvb { x = -1, y = 1, z = 1, u = 1, v = 1, r = 255, g = 255, b = 255, a = 255 };
                UInt16[] indexdata = new UInt16[6];
                indexdata[0] = 0;
                indexdata[1] = 1;
                indexdata[2] = 2;
                indexdata[3] = 2;
                indexdata[4] = 1;
                indexdata[5] = 3;
                fixed (void* vbptr = vbdata, ibptr = indexdata)
                {
                    return Framework.Mesh_Create(Framework.VertexFormat.Pos_UV0_Color0, 12, 2, (byte*)vbptr, (ushort*)0);
                }
            }
        }

    }
}
